{% extends 'base.html' %}

{% block content %}
<body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <textarea class="layui-textarea" style="height: 147px;">{{lists[3]}}</textarea>
                    <hr/>
                    <input hidden value="{{lists[0]}}" id="qid" name="qid">
                </form>
                <form style="padding-left: 10%;">
                    <button id="docker-start" type="button" class="layui-btn layui-btn-warm" lay-filter="start" lay-submit="" style="margin-left: -10%;">启动靶机环境</button>
                    <label id="url" name="url" style="display: none;margin-left: -40px;">靶机地址：
                </form>
            </div>
        </div>
<script>
    layui.use(['form', 'layer'],
        function() {
        $ = layui.jquery;
        var form = layui.form, layer = layui.layer;

        //监听docker-start按钮
            form.on('submit(start)',function () {
                var index = layer.alert('正在开启...',{closeBtn: 0, btn: 0});
                $.ajax({
                    type: "POST",
                    url: "{{ url_for('index.docker_action',qid=lists[0]) }}",
                    data: {"action":"start"},
                    success: function (start) {
                        console.log(start);//日志输出靶机URL地址->start
                        $("#docker-start").css("display","none");
                        var tmp ='<a href="'+start+'" target="_blank">'+start+'</a></label>'+'&nbsp&nbsp&nbsp'+
                        '<button id="docker-stop" type="button" class="layui-btn layui-btn-danger" lay-filter="stop" lay-submit="">关闭靶机环境</button>';
                        $("#url").append(tmp);
                        $("#url").css("display","inline");
                        layer.close(index);

                        },error:function () {
                                layer.alert('docker_action,请求失败');
                        }
                    });
                });
                
            //监听docker-stop按钮
         form.on('submit(stop)',function () {
                    layer.alert('正在关闭...',{closeBtn: 0, btn: 0});
                    $.ajax({
                        type: "POST",
                        url: "{{ url_for('index.docker_action',qid=lists[0]) }}",
                        data: {"action":"stop"},
                        success: function (stop) {
                            console.log(stop);
                            layer.alert("操作成功，靶机已经关闭", {icon: 6}, function() {

                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);

                                // 关闭当前frame
                                parent.layer.close(index);

                                // 可以对父窗口进行刷新
                                //xadmin.father_reload();
                            });
                        },error:function () {
                                layer.alert('docker_action,请求失败');
                        }
                    });
                });
            });

         

        </script>
    </body>
{% endblock %}